﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class MainUI : UI_ctrl
{
    
    void Start()
    {
        UpdateCurScore(0);
        UpdateTopScore();

        EventMgr.instance.AddListener("UpdateCurScore", UpdateCurScore);
        EventMgr.instance.AddListener("GameOver", GameOver);

        this.view["RestartBtn"].GetComponent<Button>().onClick.AddListener(() => { SceneManager.LoadScene(0); });
        this.view["QuitBtn"].GetComponent<Button>().onClick.AddListener(() => { Application.Quit(); });
    }

    private void OnDestroy()
    {
        EventMgr.instance.RemoveListener("UpdateCurScore", UpdateCurScore);
        EventMgr.instance.RemoveListener("GameOver", GameOver);
    }

    public void UpdateCurScore(object udata)
    {
        this.view["CurScoreBG/CurScoreValText"].GetComponent<Text>().text = udata.ToString();
    }

    public void UpdateTopScore()
    {
        this.view["TopScoreBG/TopScoreValText"].GetComponent<Text>().text = PlayerPrefs.GetInt("TopScore", 0).ToString();
    }

    public void GameOver(object udata)
    {
        int topScore = PlayerPrefs.GetInt("TopScore", 0);
        if ((int)udata > topScore)
        {
            PlayerPrefs.SetInt("TopScore", (int)udata);
            UpdateTopScore();
        }

    }
}
